package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.ShiftToPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CurrentTypeBean;
import com.sskj.lib.bean.ShiftBean;
import com.sskj.lib.widget.BottomDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.FUNCTIONS_SHIFT)
public class ShiftToActivity extends BaseActivity<ShiftToPresenter> {

    @BindView(R2.id.money_et)
    EditText moneyEt;
    @BindView(R2.id.shift_time)
    TextView shiftTime;
    @BindView(R2.id.shift_bt)
    Button shiftBt;
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.shift_title_tv)
    TextView shiftTitleTv;
    @BindView(R2.id.shift_to_rl)
    RelativeLayout shiftToRl;
    List<String> coins = new ArrayList();
    @BindView(R2.id.shift_money_tv)
    TextView shiftMoneyTv;
    @BindView(R2.id.shift_state_tv)
    TextView shiftStateTv;
    @BindView(R2.id.shift_all_tv)
    TextView shiftAllTv;
    @BindView(R2.id.ivShift)
    ImageView ivShift;
    @Autowired
    String currency;
    @Autowired
    String type;
    private String ptype = "1";
    private String choose = "AICASH";
    private String state = "aicash";
    private String money = "";
    private ShiftBean data;
    private CurrentTypeBean pType;

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_shift_to;
    }

    @Override
    public ShiftToPresenter getPresenter() {
        return new ShiftToPresenter();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initView() {
        mPresenter.getCurrentType();
        ARouter.getInstance().inject(this);
        setCurrency();
        if (type.equals(Constans.ZHUANRU)) {
            tvTitle.setText(App.INSTANCE.getString(R.string.functions_shiftToActivity2));
            shiftBt.setText(App.INSTANCE.getString(R.string.functions_shiftToActivity2));
            ptype = "2";
            //活期宝转入
            setRollOut();
        } else if (type.equals(Constans.ZHUANCHU)) {
            tvTitle.setText(App.INSTANCE.getString(R.string.functions_shiftToActivity1));
            shiftBt.setText(App.INSTANCE.getString(R.string.functions_shiftToActivity1));
            ptype = "1";
            //活期宝转出
            setShiftTo();
        }
//        //转入按钮
//        ClickUtil.click(500, shiftBt, () -> {
//            BottomDialog.posswordInput(this, (tv) -> {
//                ToastUtil.showShort(tv);
//            });
//        });
        //货币类型选择
        ClickUtil.click(300, shiftToRl, () -> {
            if (coins.size() == 0) {
                ToastUtils.show(App.INSTANCE.getString(R.string.walletFunctions_currentTreasureBizhong));
                return;
            }
            BottomDialog.chooseCoins(this, coins, (tv) -> {
                mPresenter.getShiftToData(tv, ptype);
                this.shiftTitleTv.setText(tv);
                this.shiftStateTv.setText(tv);
                if (shiftTitleTv.getText().toString().equals("AICASH")) {
                    ImageUtil.setImage(R.mipmap.icon_ai, ivShift);
                }
                if (shiftTitleTv.getText().toString().equals("BTC")) {
                    ImageUtil.setImage(R.mipmap.icon_btc, ivShift);
                }
                if (shiftTitleTv.getText().toString().equals("ETH")) {
                    ImageUtil.setImage(R.mipmap.icon_eth, ivShift);
                }
                if (shiftTitleTv.getText().toString().equals("LTC")) {
                    ImageUtil.setImage(R.mipmap.icon_ltc, ivShift);
                }
                if (shiftTitleTv.getText().toString().equals("USDT")) {
                    ImageUtil.setImage(R.mipmap.icon_usdt, ivShift);
                }
                choose = tv;
                state = choose.toLowerCase();
            });
        });
        //全部按钮
        ClickUtil.click(300, shiftAllTv, () -> {
            moneyEt.setText(data.getPrice());
            moneyEt.setSelection(moneyEt.getText().length());
        });
    }

    @SuppressLint("SetTextI18n")
    private void setCurrency() {
        if (currency.equals("AICASH")) {
            shiftTitleTv.setText("AICASH");
            choose = "AICASH";
            shiftStateTv.setText("AICASH");
            ImageUtil.setImage(R.mipmap.icon_ai, ivShift);
        }
        if (currency.equals("BTC")) {
            shiftTitleTv.setText("BTC");
            choose = "BTC";
            shiftStateTv.setText("BTC");
            ImageUtil.setImage(R.mipmap.icon_btc, ivShift);
        }
        if (currency.equals("ETH")) {
            shiftTitleTv.setText("ETH");
            choose = "ETH";
            shiftStateTv.setText("ETH");
            ImageUtil.setImage(R.mipmap.icon_eth, ivShift);
        }
        if (currency.equals("LTC")) {
            shiftTitleTv.setText("LTC");
            choose = "LTC";
            shiftStateTv.setText("LTC");
            ImageUtil.setImage(R.mipmap.icon_ltc, ivShift);
        }
        if (currency.equals("USDT")) {
            shiftTitleTv.setText("USDT");
            choose = "USDT";
            shiftStateTv.setText("USDT");
            ImageUtil.setImage(R.mipmap.icon_usdt, ivShift);
        }
    }

    private void setRollOut() {
        //转入按钮
        ClickUtil.click(500, shiftBt, () -> {
            if (TextUtils.isEmpty(moneyEt.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_shiftToActivity3));
                return;
            }
            double v = Double.parseDouble(moneyEt.getText().toString());
            if (v == 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_shiftToActivity4));
                return;
            } else {
                BottomDialog.posswordInput(this, (tv) -> {
                    mPresenter.getShiftTo(getText(shiftStateTv).toLowerCase(), moneyEt.getText().toString(), tv);
                });
            }
        });
    }

    private void setShiftTo() {
        //转出按钮
        ClickUtil.click(500, shiftBt, () -> {
            if (TextUtils.isEmpty(moneyEt.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_shiftToActivity3));
                return;
            }
            double v = Double.parseDouble(moneyEt.getText().toString());
            if (v == 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_shiftToActivity4));
                return;
            } else {
                BottomDialog.posswordInput(this, (tv) -> {
                    mPresenter.getRollOut(getText(shiftStateTv).toLowerCase(), moneyEt.getText().toString(), tv);
                });
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getShiftToData(shiftTitleTv.getText().toString(), type);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ShiftToActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //货币余额录入
    @SuppressLint("SetTextI18n")
    public void getShiftToData(ShiftBean data) {
        this.data = data;
        if (data != null) {
            int time = data.getTime();

            shiftTime.setText(TimeFormatUtil.formatE(Long.valueOf(time) * 1000));

            shiftMoneyTv.setText(data.getPrice() + " " + choose);

        }
    }

    public void getCoinListOK(List<String> data) {
        if (data == null) {
            return;
        }
        coins.addAll(data);
    }

    //转入成功
    public void getShiftToSuccess() {
        ARouter.getInstance().build(RConfig.FUNCTIONS_CURRENT_RECORD).navigation();
        finish();
    }
}
